package com.jlwl.houseservice.controller;

import com.jlwl.houseservice.common.Result;
import com.jlwl.houseservice.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件上传控制器
 */
@RestController
@RequestMapping("/api/files")
public class FileController {

    @Autowired
    private FileService fileService;

    /**
     * 上传单个图片
     * @param file 图片文件
     * @return 图片URL
     */
    @PostMapping("/upload/image")
    public Result<String> uploadImage(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("请选择要上传的图片");
        }
        
        try {
            String imageUrl = fileService.uploadImage(file);
            return Result.success(imageUrl);
        } catch (Exception e) {
            return Result.error("图片上传失败：" + e.getMessage());
        }
    }

    /**
     * 批量上传图片
     * @param files 图片文件列表
     * @return 图片URL列表
     */
    @PostMapping("/upload/images")
    public Result<List<String>> uploadImages(@RequestParam("files") List<MultipartFile> files) {
        if (files == null || files.isEmpty()) {
            return Result.error("请选择要上传的图片");
        }
        
        try {
            List<String> imageUrls = fileService.uploadImages(files);
            return Result.success(imageUrls);
        } catch (Exception e) {
            return Result.error("图片上传失败：" + e.getMessage());
        }
    }
} 